package com.bg.print.controller;

import com.bg.print.dto.OrderDto;
import com.bg.print.entity.OrderEntity;
import com.bg.print.service.OrderService;
import com.bg.print.utils.PageUtils;
import com.bg.print.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;


/**
 * 订单
 *
 * @author liuxvru
 * @email liuxvru@163.com
 * @date 2024-03-25 17:50:46
 */
@Tag(name = "订单")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Operation(summary = "列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);
        return R.ok().put("data", page);
    }


    @Operation(summary = "信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OrderDto order = orderService.getOrderDtoById(id);
        return R.ok().put("data", order);
    }

    @Operation(summary = "生成订单")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('BUYER')")
    public R save(@RequestBody OrderDto order, @AuthenticationPrincipal UserDetails userDetails) {
        OrderDto newOrderDto = orderService.saveOrderDto(order, userDetails);
        if (newOrderDto != null) {
            return R.ok().put("data", newOrderDto);
        }
        return R.error(500, "创建订单失败");
    }

    @Operation(summary = "申请报价")
    @PutMapping("/toqMoney/{id}")
    @PreAuthorize("hasAuthority('BUYER')")
    public R requestMoney(@PathVariable Long id, @AuthenticationPrincipal UserDetails userDetails) {
        Integer ok = orderService.requestMoney(id, userDetails);
        if (ok != 0)
            return R.ok();
        return R.error(500, "申请报价失败");
    }

    @Operation(summary = "报价")
    @PutMapping("/tookMoney/")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R oktMoney(@RequestParam("orderId") Long id, @RequestParam("money") BigDecimal money) {
        Integer ok = orderService.oktMoney(id, money);
        if (ok != 0)
            return R.ok();
        return R.error(500, "报价失败");
    }


    @Operation(summary = "正在打印")
    @PutMapping("/toprinting/{id}")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R toPrinting(@PathVariable Long id) {
        Integer ok = orderService.toPrinting(id);
        if (ok != 0)
            return R.ok();
        return R.error(500, "操作失败");
    }

    @Operation(summary = "打印完成")
    @PutMapping("/okprint/{id}")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R okPrint(@PathVariable Long id) {
        Integer ok = orderService.okPrint(id);
        if (ok != 0)
            return R.ok();
        return R.error(500, "操作失败");
    }

    @Operation(summary = "订单完成")
    @PutMapping("/okOrder/{id}")
    @PreAuthorize("hasAuthority('BUYER')")
    public R okOrder(@PathVariable Long id, @AuthenticationPrincipal UserDetails userDetails) {
        Integer ok = orderService.okOrder(id, userDetails);
        if (ok != 0)
            return R.ok();
        return R.error(500, "订单完成失败");
    }

    @Operation(summary = "申请取消订单")
    @PutMapping("/toqcancel/{id}")
    @PreAuthorize("hasAuthority('BUYER')")
    public R qcancelOrder(@PathVariable Long id, @AuthenticationPrincipal UserDetails userDetails) {
        Integer ok = orderService.qcancelOrder(id, userDetails);
        if (ok != 0)
            return R.ok();
        return R.error(500, "申请失败");
    }

    @Operation(summary = "取消取消订单")
    @PutMapping("/giveUpcancel/{id}")
    @PreAuthorize("hasAuthority('BUYER')")
    public R cancelCancelOrder(@PathVariable Long id, @AuthenticationPrincipal UserDetails userDetails) {
        Integer ok = orderService.cancelCancelOrder(id, userDetails);
        if (ok != 0)
            return R.ok();
        return R.error(500, "申请失败");
    }
//    @Operation(summary = "同意取消订单")
//    @PutMapping("/tookcancel/{id}")
//    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
//    public R okcancel(@PathVariable Long id) {
//        Integer ok = orderService.okcancelOrder(id);
//        if (ok != 0)
//            return R.ok();
//        return R.error(500, "取消失败");
//    }
    @Operation(summary = "驳回取消订单")
    @PutMapping("/tonocancel/{id}")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('MERCHANT')")
    public R nocancelOrder(@PathVariable Long id) {
        Integer ok = orderService.nocancelOrder(id);
        if (ok != 0)
            return R.ok();
        return R.error(500, "驳回失败");
    }

    @Operation(summary = "删除(删除自己的订单)")
    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('BUYER')")
    public R delete(@AuthenticationPrincipal UserDetails userDetails, @RequestBody Long[] ids) {
        Integer ok = orderService.deleteByIds(Arrays.asList(ids), userDetails);
        if (ok != 0)
            return R.ok();
        return R.error(500, "删除失败");
    }

    @Operation(summary = "查询自己的所有订单")
    @GetMapping("/my")
    @PreAuthorize("hasAuthority('BUYER')")
    public R getAllSelf(@AuthenticationPrincipal UserDetails userDetails, @RequestParam Map<String, Object> params) {
        PageUtils orderDtos = orderService.getMyOrder(userDetails, params);
        return R.ok().put("data", orderDtos);
    }

//    @Operation(summary = "修改")
//    @PutMapping("/update")
//    @PreAuthorize("hasAuthority('ADMIN')")
//    public R update(@RequestBody OrderEntity order) {
//        orderService.updateById(order);
//        return R.ok();
//    }

}
